package com.example.serverdemo.controller;

import com.example.serverdemo.common.Constant;
import com.example.serverdemo.netty.device_tcp_server.api.DeviceAPI;
import com.example.serverdemo.utils.log.PrintLogUtil;
import com.example.serverdemo.utils.redisutils.RedisUtils;
import com.example.serverdemo.utils.sessionutils.session.DeviceCommunicationSession;
import com.example.serverdemo.utils.sessionutils.sessionmanager.DeviceCommunicationSessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DeviceController {
    @Autowired
    private RedisUtils redisUtils;

    @ResponseBody
    @RequestMapping(value = "/device/restart",method = RequestMethod.POST)
    public boolean restartDevice(String deviceId){
        try {
            DeviceCommunicationSession session = DeviceCommunicationSessionManager.getSession(deviceId);
            DeviceAPI api = (DeviceAPI) session.getAttribute(Constant.DEVICE_HANDLER);
            return api.restart();
        }catch (Exception e){
            PrintLogUtil.printError(this,e);
        }
        return false;
    }
    @ResponseBody
    @RequestMapping(value = "/device/ota",method = RequestMethod.POST)
    public Boolean deviceOTA(String deviceId, String filePath){
        try {
            redisUtils.set(Constant.PROGRESS + deviceId,"0");
            DeviceCommunicationSession session = DeviceCommunicationSessionManager.getSession(deviceId);
            DeviceAPI api = (DeviceAPI) session.getAttribute(Constant.DEVICE_HANDLER);
            return api.remoteUpdate(filePath);
        } catch (Exception e) {
            PrintLogUtil.printError(this,e);
        }
        redisUtils.set(Constant.PROGRESS + deviceId,"-1");
        return false;
    }
    @ResponseBody
    @RequestMapping(value = "/device/configurationHardware",method = RequestMethod.POST)
    public boolean deviceConfigurationHardwareParameter(String deviceId){
        try {
            DeviceCommunicationSession session = DeviceCommunicationSessionManager.getSession(deviceId);
            DeviceAPI api = (DeviceAPI) session.getAttribute(Constant.DEVICE_HANDLER);
            return api.configurationHardwareParameter();
        }catch (Exception e){
            PrintLogUtil.printError(this,e);
        }
        return false;
    }
    @ResponseBody
    @RequestMapping(value = "/device/readBackLightControllerParameter",method = RequestMethod.POST)
    public boolean readBackLightControllerParameter(String deviceId){
        try {
            DeviceCommunicationSession session = DeviceCommunicationSessionManager.getSession(deviceId);
            DeviceAPI api = (DeviceAPI) session.getAttribute(Constant.DEVICE_HANDLER);
            return api.readBackLightControllerParameter();
        }catch (Exception e){
            PrintLogUtil.printError(this,e);
        }
        return false;
    }
    @ResponseBody
    @RequestMapping(value = "/device/configurationLightControllerParameter",method = RequestMethod.POST)
    public boolean configurationLightControllerParameter(String deviceId,String parameterJsonStr){
        try {
            DeviceCommunicationSession session = DeviceCommunicationSessionManager.getSession(deviceId);
            DeviceAPI api = (DeviceAPI) session.getAttribute(Constant.DEVICE_HANDLER);
            return api.configurationLightControllerParameter(parameterJsonStr);
        }catch (Exception e){
            PrintLogUtil.printError(this,e);
        }
        return false;
    }
    @ResponseBody
    @RequestMapping(value = "/device/configurationProtocol",method = RequestMethod.POST)
    public boolean configurationProtocol(String deviceId,int protocolId){
        try {
            DeviceCommunicationSession session = DeviceCommunicationSessionManager.getSession(deviceId);
            DeviceAPI api = (DeviceAPI) session.getAttribute(Constant.DEVICE_HANDLER);
            return api.configurationCentralControllerProtocol(protocolId);
        }catch (Exception e){
            PrintLogUtil.printError(this,e);
        }
        return false;
    }
    @ResponseBody
    @RequestMapping(value = "device/test",method = RequestMethod.POST)
    public boolean test(){
        System.out.println("this is a test");
        return true;
    }
}
